package com.lan.schedule;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;

public class tjactivity extends Activity {
    int jie1;
    int day1;
    CheckBox che;
    CheckBox[] cch = new CheckBox[25];

    class BoxCheckListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!isChecked) {
                che.setChecked(isChecked);
            } else {
                boolean bbl = true;
                for (int j = 0; j < 25; j++) {
                    bbl = bbl & cch[j].isChecked();
                }
                if (bbl) {
                    che.setChecked(isChecked);
                }
            }
        }
    }

    class day implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.x1:
                    day1 = 2;
                    break;
                case R.id.x2:
                    day1 = 3;
                    break;
                case R.id.x3:
                    day1 = 4;
                    break;
                case R.id.x4:
                    day1 = 5;
                    break;
                case R.id.x5:
                    day1 = 6;
                    break;
                case R.id.x6:
                    day1 = 0;
                    break;
                case R.id.x7:
                    day1 = 1;
                    break;
            }
        }

    }
    class jie implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.j1:
                    jie1 = 1;
                    break;
                case R.id.j2:
                    jie1 = 2;
                    break;
                case R.id.j3:
                    jie1 = 3;
                    break;
                case R.id.j4:
                    jie1 = 4;
                    break;
                case R.id.j5:
                    jie1 = 5;
                    break;
            }
        }

    }

    class AllCheckListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            CheckBox all = (CheckBox) v;
            for (int i = 0; i < 25; i++) {
                cch[i].setChecked(all.isChecked());
            }
        }
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Window window = getWindow();

            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            window.setStatusBarColor(Color.TRANSPARENT);

            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.tj_activity);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        Intent intent = getIntent();
        final int g=intent.getIntExtra("g",0);
        int dayt = intent.getIntExtra("day",0);
        int jiet = intent.getIntExtra("jie",0);
        String loct=intent.getStringExtra("loc");
        String namet=intent.getStringExtra("name");
        final int we=intent.getIntExtra("we",0);

        int[] week = new int[0];
        if(we==0);else{
            week=intent.getIntArrayExtra("week");
            TextView he=findViewById(R.id.head);
            he.setText("编辑课程");
            Button qd=findViewById(R.id.tj);
            qd.setText("确定");
        }
        day dc=new day();
        jie jc=new jie();
        RadioButton x1=findViewById(R.id.x1);
        x1.setOnClickListener(dc);
        RadioButton x2=findViewById(R.id.x2);
        x2.setOnClickListener(dc);
        RadioButton x3=findViewById(R.id.x3);
        x3.setOnClickListener(dc);
        RadioButton x4=findViewById(R.id.x4);
        x4.setOnClickListener(dc);
        RadioButton x5=findViewById(R.id.x5);
        x5.setOnClickListener(dc);
        RadioButton x6=findViewById(R.id.x6);
        x6.setOnClickListener(dc);
        RadioButton x7=findViewById(R.id.x7);
        x7.setOnClickListener(dc);
        RadioButton[] ridx={x6,x7,x1,x2,x3,x4,x5};
        RadioButton j1=findViewById(R.id.j1);
        j1.setOnClickListener(jc);
        RadioButton j2=findViewById(R.id.j2);
        j2.setOnClickListener(jc);
        RadioButton j3=findViewById(R.id.j3);
        j3.setOnClickListener(jc);
        RadioButton j4=findViewById(R.id.j4);
        j4.setOnClickListener(jc);
        RadioButton j5=findViewById(R.id.j5);
        j5.setOnClickListener(jc);
        RadioButton[] redj={j1,j2,j3,j4,j5};
        ridx[dayt].setChecked(true);
        day1=dayt;
        redj[jiet-1].setChecked(true);
        jie1=jiet;
        int[] id = {R.id.c1, R.id.c2, R.id.c3, R.id.c4, R.id.c5, R.id.c6, R.id.c7, R.id.c8, R.id.c9, R.id.c10, R.id.c11, R.id.c12, R.id.c13, R.id.c14, R.id.c15, R.id.c16, R.id.c17, R.id.c18, R.id.c19, R.id.c20, R.id.c21, R.id.c22, R.id.c23, R.id.c24, R.id.c25};
        che = findViewById(R.id.c0);
        final EditText et1 = findViewById(R.id.name);
        final EditText et2 = findViewById(R.id.loc);
        et1.setText(namet);
        et2.setText(loct);
        boolean ifk=true;
        if(namet.equals("")){
            ifk=false;
        }
        AllCheckListener allCheckListener = new AllCheckListener();
        che.setOnClickListener(allCheckListener);
        BoxCheckListener boxCheckListener = new BoxCheckListener();
        for (int i = 0; i < 25; i++) {
//            System.out.println("888");
            cch[i] = findViewById(id[i]);
            cch[i].setOnCheckedChangeListener(boxCheckListener);
        }
        if(we!=0){
        for(int i=1;i<=we;i++){
            cch[week[i]-1].setChecked(true);
        }}
        Button qr = findViewById(R.id.tj);
        final Button qx = findViewById(R.id.qx);
        qr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(we==0){
                int we=0;
                String week="";
                for(int i=0;i<25;i++){
                    if(cch[i].isChecked()){
                        we++;
                        int i1=i+1;
                        week=week+i1+"\n";
                    }
                }
                String name = String.valueOf(et1.getText());
                String loc = String.valueOf(et2.getText());
                String s1 = "";
                String s2 = "";
                int j = 0;
                try {
                    String s;
                    FileInputStream fileInputStream = openFileInput("test.txt");
                    FileChannel f1 = fileInputStream.getChannel();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
                    j = Integer.parseInt(bufferedReader.readLine());
                    while (((s = bufferedReader.readLine())!=null) &&!(s.equals("")) ) {
                        s1 = s1 + s + "\n";
                    }
                    f1.close();
                    bufferedReader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                j = j + 1;
                s2 = j + "\n" + s1 + name + "\n" + loc + "\n"+jie1+"\n"+day1+"\n"+we+"\n"+week;
                try {
                    FileOutputStream fileOutputStream = openFileOutput("test.txt", 0);
                    fileOutputStream.write(s2.getBytes());
                    fileOutputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }}else{
                    int we12=0;
                    String week12="";
                    for(int i=0;i<25;i++){
                        if(cch[i].isChecked()){
                            we12++;
                            int i1=i+1;
                            week12=week12+i1+"\n";
                        }
                    }
                    int j = 0;
                    String name1[] = new String[50];
                    String loc[] = new String[50];
                    int week[][] = new int[50][30];
                    int we[] = new int[50];
                    int day[] = new int[50];
                    int jie[] = new int[50];
                    try {
                        FileInputStream fileInputStream = openFileInput("test.txt");
                        FileChannel f1 = fileInputStream.getChannel();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
                        j = Integer.parseInt(bufferedReader.readLine());
                        for (int i = 1; i < j; i++) {
                            name1[i] = bufferedReader.readLine();
                            loc[i] = bufferedReader.readLine();
                            jie[i] = Integer.parseInt(bufferedReader.readLine());
                            day[i] = Integer.parseInt(bufferedReader.readLine());
                            we[i] = Integer.parseInt(bufferedReader.readLine());
                            for (int p = 1; p <= we[i]; p++) {
                                week[i][p] = Integer.parseInt(bufferedReader.readLine());
                            }
                        }
                        f1.close();
                        bufferedReader.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    String data = j + "\n";
                    for (int i = 1; i < j; i++) {
                        if(i!=g){
                            data = data + name1[i] + "\n" + loc[i] + "\n" + jie[i] + "\n" + day[i] + "\n" + we[i] + "\n";
                            for (int p = 1; p <= we[i]; p++) {
                                data = data + week[i][p] + "\n";
                            }}else{
                            String name12 = String.valueOf(et1.getText());
                            String loc12 = String.valueOf(et2.getText());
                            data=data+name12+"\n"+loc12+"\n"+jie1+"\n"+day1+"\n"+we12+"\n";
                            data=data+week12;
                        }
                    }
                    try {
                        FileOutputStream fileOutputStream = openFileOutput("test.txt", 0);
                        fileOutputStream.write(data.getBytes());
                        fileOutputStream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                MyAppliacation.getInstance().getHandler().sendEmptyMessage(1);
                tjactivity.this.finish();
            }
        });
        qx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tjactivity.this.finish();
            }
        });
    }
}
